package com.example.wanjinyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.wanjinyou.mapper.CommentMapper;
import com.example.wanjinyou.mapper.UserMapper;
import com.example.wanjinyou.pojo.entity.Comment;
import com.example.wanjinyou.pojo.entity.User;
import com.example.wanjinyou.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.wanjinyou.service.UserService;
import com.example.wanjinyou.vo.CommentVo;
import com.example.wanjinyou.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.wanjinyou.vo.Result;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr.Chen
 * @since 2022-10-20
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    CommentMapper commentMapper;
    /**
     * 获取评论列表
     * @param articleId
     * @return
     */
    @Override
    public Result commentsByArticleId(Long articleId) {
        /*
          1. 根据文章id 在评论表中查找对应的评论
          2. 根据每条评论作者的id ,查找对应的用户信息
          3. 判断评论的等级 , 如果level = 1 ,查询有没有子评论 ,如果有根据 评论id查找 子评论
         */
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq( Comment::getArticleId,articleId); //根据文章id查询对应的评论
        queryWrapper.eq(Comment::getLevel,1);        //先查询等级为1的评论
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        //转换为vo对象
        List<CommentVo> commentVos = copyList(comments);
        return Result.success(commentVos);
    }

    //vo对象批量转换
    private List<CommentVo> copyList(List<Comment> comments) {
        List<CommentVo> commentVoList = new ArrayList<>();
        for (Comment comment: comments) {
            commentVoList.add( copy(comment) );
        }
        return commentVoList;
    }

    @Autowired
    UserService userService;

    private CommentVo copy(Comment comment) {
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment,commentVo); //将类型相同属性拷贝进去
        //设置作者信息
        UserVo userVo = userService.findUserVoById(comment.getAuthorId());
        commentVo.setAuthor(userVo);
        //如果leave = 1, 查询有无 子评论
        Integer level = comment.getLevel();
        if(  1 == level) {
            Long id = comment.getId();
            //根据父评论id 查找子评论
            List<CommentVo> childrenCommentList = findCommentByParentId(id);
            commentVo.setChildrens(childrenCommentList);
        }
        //>1说明是子评论, 查找是给谁的评论,设置到commentVo中
        if( level>1 ) {
            Long toUid = comment.getToUid();
            UserVo toUserVo = userService.findUserVoById(toUid);
            commentVo.setToUser(toUserVo); //将评论的 那条评论的用户
        }
        return commentVo;
    }

    //根据父评论id,查找子评论,并转换为vo对象返回
    private List<CommentVo> findCommentByParentId(Long id) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper();
        //select * from comment where parentid = ?;
        queryWrapper.eq(Comment::getParentId,id);
        queryWrapper.eq(Comment::getLevel,2);
        List<Comment> commentList = commentMapper.selectList(queryWrapper);
        List<CommentVo> commentVoList = copyList(commentList);
        return  commentVoList;
    }
}
